package com.uenpay.im.ui.old.activity.stickers;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-1 下午3:02:53
 * @version 1.0
 * @function 自定义表情管理
 *
 */
public class StickersHandle {

	public interface IAddStickerListener {
		void onAddStrickerResult(int status, String msg,
												 String favoriteid);
	}

	/**
	 * 添加表情
	 *
	 * @param customFaceJson
	 * @param mIAddStickerListener
	 */
	public static void addSticker(String customFaceJson,
			final IAddStickerListener mIAddStickerListener) {
		AddStickerInvokItem item = new AddStickerInvokItem(customFaceJson);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							AddStickerInvokItem.AddStickerInvokItemResult result = ((AddStickerInvokItem) resultObject)
									.getOutput();
							if (result != null) {
								if (result.status == HttpInvokeResultCode.RESULT_OK) {
									getCustomFaceList(null);
								}
								if (mIAddStickerListener != null) {
									mIAddStickerListener.onAddStrickerResult(
											result.status, result.msg,
											result.favoriteid);
								}
							}
						} else {
							mIAddStickerListener
									.onAddStrickerResult(-1, "", "");
						}
					}
				});
	}

	public interface IDeleteCustomListener {
		void onDeleteResult(int status, String msg);
	}

	/**
	 * 删除表情
	 *
	 * @param favoriteids
	 * @param mIDeleteCustomListener
	 */
	public static void deleteCustomFace(String favoriteids,
			final IDeleteCustomListener mIDeleteCustomListener) {
		DeleteStickerInvokItem item = new DeleteStickerInvokItem(favoriteids);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteStickerInvokItem.DeleteStickerInvokItemResult result = ((DeleteStickerInvokItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIDeleteCustomListener != null)
									mIDeleteCustomListener.onDeleteResult(
											result.status, result.msg);
							}
						} else {
							mIDeleteCustomListener.onDeleteResult(-1, "");
						}
					}
				});
	}

	public interface IGetCustomFaceListener {
		void onGetCustomFaceResult(int status, String msg,
												   ArrayList<StickerEntity> arrayList);
	}

	/**
	 * 获取表情列表数据
	 *
	 * @param getCustomFaceListener
	 */
	public static void getCustomFaceList(
			final IGetCustomFaceListener getCustomFaceListener) {
		GetStickersListInvokItem item = new GetStickersListInvokItem();
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetStickersListInvokItem.GetStickersListInvokItemResult result = ((GetStickersListInvokItem) resultObject)
									.getOutput();
							if (result != null) {
								saveCustomFaceToLocal(result.arrayList);
								if (getCustomFaceListener != null) {
									getCustomFaceListener
											.onGetCustomFaceResult(
													result.status, result.msg,
													result.arrayList);
								}
							}
						} else {
							if (getCustomFaceListener != null) {
								getCustomFaceListener.onGetCustomFaceResult(-1,
										"", null);
							}
						}
					}
				});
	}

	/**
	 * 将服务器表情数据同步到本地
	 *
	 * @param arrayList
	 */
	private static void saveCustomFaceToLocal(ArrayList<StickerEntity> arrayList) {
		if (arrayList != null && arrayList.size() > 0) {
			JSONArray jsonArray = new JSONArray();
			for (int i = 0, size = arrayList.size(); i < size; i++) {
				JSONObject json = new JSONObject();
				try {
					json.put("fileid", arrayList.get(i).fileId);
					json.put("filehash", arrayList.get(i).fileHash);
					json.put("customefaceurl", arrayList.get(i).customeFaceUrl);
					json.put("favoriteid", arrayList.get(i).favoriteid);
					jsonArray.put(json);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			IMApplication.config.setCustomFaceJson(IMApplication.getCustId(),
					jsonArray.toString());

		} else {
			// 不存在收藏的表情
			IMApplication.config.setCustomFaceJson(IMApplication.getCustId(),
					"");
		}
	}
}
